package com.xuhao.examai.scoring.infrastructure.persistence;

import com.xuhao.examai.scoring.domain.models.ExamRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Repository
public interface ExamRecordJpaRepository extends JpaRepository<ExamRecord, Long> {

	List<ExamRecord> findByExamId(Long examId);

	List<ExamRecord> findByStudentId(Long studentId);

	List<ExamRecord> findByStatus(String status);

	Optional<ExamRecord> findByExamIdAndStudentId(Long examId, Long studentId);

	@Query("SELECT er FROM ExamRecord er WHERE er.examId = :examId AND er.status = :status")
	List<ExamRecord> findByExamIdAndStatus(@Param("examId") Long examId, @Param("status") String status);

	@Query("SELECT er FROM ExamRecord er WHERE er.studentId = :studentId AND er.status = :status")
	List<ExamRecord> findByStudentIdAndStatus(@Param("studentId") Long studentId, @Param("status") String status);

	@Query("SELECT er FROM ExamRecord er WHERE er.startTime BETWEEN :startTime AND :endTime")
	List<ExamRecord> findByStartTimeBetween(@Param("startTime") LocalDateTime startTime,
			@Param("endTime") LocalDateTime endTime);

	@Query("SELECT COUNT(er) FROM ExamRecord er WHERE er.examId = :examId AND er.status = :status")
	Long countByExamIdAndStatus(@Param("examId") Long examId, @Param("status") String status);
}